package cn.csc.module.pay.util;

import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * @author liuchuang
 * @version 1.0
 * @date 2023/8/22 17:33
 * @modify
 */
@Slf4j
public class NetUtils {

    public static String getMac(){
        try {
            Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
            while (enumeration.hasMoreElements()) {
                NetworkInterface networkInterface = (NetworkInterface) enumeration.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isPointToPoint() || networkInterface.isVirtual()) {
                    continue;
                }
                if (networkInterface.isUp()) {
                    byte[] arrayOfByte = networkInterface.getHardwareAddress();
                    if (arrayOfByte != null && arrayOfByte.length == 6) {
                        StringBuilder sb = new StringBuilder();
                        for (byte b = 0; b < arrayOfByte.length; b++) {
                            if (b != 0) {
                                sb.append(":");
                            }
                            sb.append(String.format("%02x", arrayOfByte[b]));
                        }
                        return sb.toString();
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }

}
